package com.naiterui.ehp.bs.doctor.controller;

import javax.validation.constraints.NotNull;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.naiterui.common.repository.page.PageVO;
import com.naiterui.ehp.bp.bo.doctor.product.ProductInfoBO;
import com.naiterui.ehp.bp.bo.doctor.product.ProductSearchParamBO;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.utils.JsonMapper;
import com.naiterui.ehp.bs.doctor.fegin.B2cFeignClient;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 医生常用药
 * 
 * @author yangfuya
 * @since 2022/3/29
 */
@Slf4j
@Api(tags = {SwaggerTag.DOCTOR + SwaggerTag.MEDICINE_DRUG})
@RestController
@RequiredArgsConstructor
@RequestMapping("ad/common/drug")
public class AdCommonDrugController {
    private final B2cFeignClient b2cFeignClient;

    @ApiOperation(value = "医生常用药列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "doctorId", value = "医生ID", required = true, dataType = "long", paramType = "query"),
        @ApiImplicitParam(name = "page", value = "页数", required = true, dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "num", value = "每页展示的数量", required = true, dataType = "int", paramType = "query")})
    @GetMapping("/list")
    public PageVO<ProductInfoBO> list(ProductSearchParamBO productSearch) {
        log.info("医生常用药我的列表: {}", JsonMapper.toJson(productSearch));
        PageVO<ProductInfoBO> pageVO = b2cFeignClient.list(productSearch);
        log.info("医生常用药我的列表result: {}", JsonMapper.toJson(pageVO.getResult()));
        // 处理客户端模型转换问题
        pageVO.getResult().forEach(s -> {
            s.setStockNum(s.getQuantity().intValue());
            s.setQuantity(1L);
        });
        return pageVO;
    }

    @ApiOperation(value = "医生常用药使用列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "doctorId", value = "医生ID", required = true, dataType = "long", paramType = "query"),
        @ApiImplicitParam(name = "page", value = "页数", required = true, dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "num", value = "每页展示的数量", required = true, dataType = "int", paramType = "query")})
    @GetMapping("/use/list")
    public PageVO<ProductInfoBO> useList(ProductSearchParamBO productSearch) {
        log.info("医生常用药我使用列表: {}", JsonMapper.toJson(productSearch));
        PageVO<ProductInfoBO> pageVO = b2cFeignClient.useList(productSearch);
        log.info("医生常用药我使用列表result: {}", JsonMapper.toJson(pageVO.getResult()));
        // 处理客户端模型转换问题
        pageVO.getResult().forEach(s -> {
            s.setStockNum(s.getQuantity().intValue());
            s.setQuantity(1L);
        });
        return pageVO;
    }

    @ApiOperation(value = "添加常用药")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "doctorId", value = "医生ID", required = true, dataType = "long", paramType = "query"),
        @ApiImplicitParam(name = "productId", value = "药品ID", required = true, dataType = "long", paramType = "query")})
    @PostMapping("/add")
    public void addCommonDrug(@RequestParam @NotNull(message = "doctorId不能为null") Long doctorId,
        @RequestParam @NotNull(message = "productId不能为null") Long productId) throws BusinessException {
        log.info("添加常用药请求参数, doctorId = {}, productId = {}", doctorId, productId);
        this.b2cFeignClient.addCommonDrug(doctorId, productId);
    }

    @ApiOperation(value = "移除常用药")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "doctorId", value = "医生ID", required = true, dataType = "long", paramType = "query"),
        @ApiImplicitParam(name = "productId", value = "药品ID", required = true, dataType = "long", paramType = "query")})
    @PostMapping("/remove")
    public void removeCommonDrug(@RequestParam @NotNull(message = "doctorId不能为null") Long doctorId,
        @RequestParam @NotNull(message = "productId不能为null") Long productId) throws BusinessException {
        log.info("移除常用药请求参数, doctorId = {}, productId = {}", doctorId, productId);
        this.b2cFeignClient.removeCommonDrug(doctorId, productId);
    }
}
